//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#pragma package(smart_init)


//---------------------------------------------------------------------------
#include "CReference.h"
//---------------------------------------------------------------------------
//                          CONSTRUCTEUR
//---------------------------------------------------------------------------
__fastcall CReference::CReference(  const int &nLogiciel,
                                    const int &nVersion)
{
    try {
        m_nLogiciel   = nLogiciel;
        m_nVersion    = nVersion;
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//                      AJOUTER DES ADRESSES
//---------------------------------------------------------------------------
void __fastcall CReference::Ajouter(const AnsiString &asLogiciel,
                                    const AnsiString &asVersion,
                                    const AnsiString &asRef)
{
    TdmWebHelp * dmWebHelp = NULL;
    TAjoutAdresse * fAjoutAdresse = NULL;
    TRefMultiple * fRefMultiple = NULL;
    try {
        dmWebHelp = new TdmWebHelp((TComponent*)0);
        bool bAffichageDesignationManquante = false;
        bool bValidation = false;
        int nIdxCbAttente = AUCUNE_SELECTION;
        AnsiString RefNom = "";
        AnsiString asDesignation = "";
        AnsiString asPageWeb = "";

        fAjoutAdresse = new TAjoutAdresse(m_nLogiciel,m_nVersion,asLogiciel,asVersion,asRef,Application);

        if(fAjoutAdresse->ShowModal()== mrOk) {
            if(fAjoutAdresse->cbRef->ItemIndex != AUCUNE_SELECTION)
                RefNom = fAjoutAdresse->cbRef->Items->Strings[fAjoutAdresse->cbRef->ItemIndex];
            else
                RefNom = fAjoutAdresse->cbRef->Text;
            asDesignation   = fAjoutAdresse->eDesignation->Text;
            asPageWeb       = fAjoutAdresse->mPageWeb->Text;
            nIdxCbAttente   = fAjoutAdresse->cbAttente->ItemIndex;
            bValidation     = true;
        }

        if(CompterReference(RefNom) != 0) {
            //Verifie la reference déjà stocké dans la base
            if(VerifierDesignation(RefNom)) {
                bAffichageDesignationManquante = true;
                bValidation = false;
            }else {
                if( asDesignation != "" )
                    bValidation = true;
                else
                    bAffichageDesignationManquante = true;
            }
        }

        if(bAffichageDesignationManquante) {
            //Ouverture de la fenetre des designations manquante
            fRefMultiple = new TRefMultiple(CompterReference(RefNom),
                                            m_nLogiciel,
                                            m_nVersion,
                                            RefNom,
                                            asDesignation,
                                            asPageWeb,
                                            nIdxCbAttente,
                                            (TComponent*)0);
            fRefMultiple->ShowModal();
        }else {
            if(bValidation) {
                dmWebHelp->tRefer->Active = true;
                dmWebHelp->tRefer->Insert();
                ntRefer(LOG_CODE)           = m_nLogiciel;
                ntRefer(VER_CODE)           = m_nVersion;
                astRefer(REF_NOM)           = RefNom;
                if(asDesignation != "")
                    astRefer(REF_SOUS_NOM)  = asDesignation;
                astRefer(REF_ADRESSE)       = asPageWeb;
                ntRefer(REF_ATTENTE)        = nIdxCbAttente;
                dmWebHelp->tRefer->Post();
                dmWebHelp->trWebHelp->Commit();
            }
        }

        CHistorique * Hist = NULL;
        Hist = new CHistorique();
        Hist->AjouterOperation(Histo_AJOUT_REF,
                                asLogiciel,
                                asVersion,
                                RefNom,
                                asDesignation,
                                asPageWeb);
        if(Hist) {delete Hist; Hist = NULL;}

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(fRefMultiple)    { delete fRefMultiple; fRefMultiple = NULL;}
    if(fAjoutAdresse)   {delete fAjoutAdresse; fAjoutAdresse = NULL;}
    if(dmWebHelp)       {delete dmWebHelp; dmWebHelp = NULL;}
}
//---------------------------------------------------------------------------
//                      MODIFIER LES ADRESSES
//---------------------------------------------------------------------------
void __fastcall CReference::Modifier(   const AnsiString &asLogiciel,
                                        const AnsiString &asVersion,
                                        TStringList * lRefCode,
                                        const int &nCount)
{
    TvAModifierAdresse * fModifierAdresse = NULL;
    try {
        fModifierAdresse = new TvAModifierAdresse(asLogiciel,asVersion,lRefCode,nCount,Application);
        fModifierAdresse->ShowModal();
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(fModifierAdresse) {delete fModifierAdresse; fModifierAdresse = NULL;}
}
//---------------------------------------------------------------------------
//                  SUPPRESSION DES ADRESSES SELECTIONNEES
//---------------------------------------------------------------------------
void __fastcall CReference::Suppression(const AnsiString &asLogiciel,
                                        const AnsiString &asVersion,
                                        const AnsiString &asRefNom,
                                        const int &nRefer)
{
    TSupprimerAdresse * fSupprAdresse = NULL;
    try {
        int nNbreRef = 0;
        nNbreRef = CompterReference(asRefNom);
        if(nNbreRef > 1){
            SupprimerAdresse(nRefer);
        }else {
            fSupprAdresse = new TSupprimerAdresse(asLogiciel,asVersion,m_nLogiciel,m_nVersion,asRefNom, Application);
            fSupprAdresse->ShowModal();
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(fSupprAdresse) {delete fSupprAdresse; fSupprAdresse = NULL;}
}
//---------------------------------------------------------------------------
//                      SUPPRIMER UNE ADRESSE
//---------------------------------------------------------------------------
void __fastcall CReference::SupprimerAdresse(const int &nRefCode)
{
    TdmWebHelp * dmWebHelp = NULL;
    TZZMySqlQuery * MaRequete = NULL;
    try {
        dmWebHelp = new TdmWebHelp((TComponent*)0);
        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text =  (AnsiString)"delete from refer where ref_code ="+nRefCode;
        MaRequete->ExecSql();
        MaRequete->Sql->Clear();

        MaRequete->Sql->Text = "Commit";
        MaRequete->ExecSql();
        MaRequete->Sql->Clear();

        MaRequete->Close();
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
}
//---------------------------------------------------------------------------
//                  COMPTER LE NOMBRE DE REFERENCES
//---------------------------------------------------------------------------
int __fastcall CReference::CompterReference( const AnsiString &asRef )
{
    TdmWebHelp * dmWebHelp = NULL;
    TZZMySqlQuery * MaRequete = NULL;
    int NombreDeRef = 0;
    try {
        dmWebHelp = new TdmWebHelp((TComponent*)0);
        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text =  (AnsiString)"select count(ref_nom) "+
                                            "from refer "+
                                            "where (log_code = "+m_nLogiciel+
                                                " and ver_code= "+m_nVersion+
                                                ") and ref_nom = "+QuotedStr(asRef)+
                                                " and ref_attente = -1";
        MaRequete->Open();
        MaRequete->First();

        NombreDeRef = nRequete(0);

        MaRequete->Sql->Clear();
        MaRequete->Close();

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        NombreDeRef = 0;}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
    return NombreDeRef;
}
//---------------------------------------------------------------------------
//                       COPIER UNE REFERENCE
//---------------------------------------------------------------------------
bool __fastcall CReference::Copier( const int &nRefer )
{
    bool bRetour = false;
    TCopyRef * fCopyRef = NULL;
    try {

        fCopyRef = new TCopyRef(nRefer,m_nLogiciel,m_nVersion,Application);
        if(fCopyRef->VerifierAdresse()) {
            if( fCopyRef->ShowModal() == mrOk )
                bRetour = true;
        }else
            MessageDlg("Aucune adresse Web enregistrée pour cette référence.\nVeuillez inserer une adresse Web.", mtWarning, TMsgDlgButtons() << mbOK, 0);

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        bRetour = false;}
    if(fCopyRef) {delete fCopyRef; fCopyRef = NULL;}
    return bRetour;
}
//---------------------------------------------------------------------------
//                      CONSULTER UNE PAGE WEB
//---------------------------------------------------------------------------
void __fastcall CReference::Consulter( const int &nRefer )
{
    TdmWebHelp * dmWebHelp = NULL;
    CVerifInternet * VerifInternet = NULL;
    try {
        dmWebHelp = new TdmWebHelp((TComponent*)0);
        AnsiString PageWeb = "";
        dmWebHelp->tRefer->Active = true;
        dmWebHelp->tRefer->First();
        while(!dmWebHelp->tRefer->Eof) {
            if(ntRefer(REF_CODE) == nRefer ) {
                PageWeb = astRefer(REF_ADRESSE);
                break;
            }
            dmWebHelp->tRefer->Next();
        }

        VerifInternet = new CVerifInternet();
        if(!PageWeb.IsEmpty()) {
            if(VerifInternet->ConnexionAuSite(PageWeb))
                ShellExecute(NULL, "Open", PageWeb.c_str(),NULL, NULL, SW_SHOWMAXIMIZED);
        }else
            MessageDlg("La mise à jour de la page Web sélectionner n'a pas été effectuée.", mtInformation, TMsgDlgButtons() << mbOK, 0);
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(VerifInternet) {delete VerifInternet; VerifInternet = NULL;}
    if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
}
//---------------------------------------------------------------------------
//                          FILTRER
//---------------------------------------------------------------------------
AnsiString __fastcall CReference::Filtrer(const int &nRefChoix,
                                          const int &nDesChoix,
                                          const int &nWebChoix,
                                          const int &nWebAttente,
                                          const AnsiString &asRef,
                                          const AnsiString &asDes,
                                          const AnsiString &asWeb)
{
    AnsiString asFiltre = "";
    try {
        AnsiString asWhere = "";
        if( m_nLogiciel == 0 )
            asWhere = " where ";
        else
            asWhere = "and ";

        //Reference
        if(!asRef.IsEmpty()) {
            switch(nRefChoix) {
                case EGALE_A:
                    asFiltre += asWhere+" ref_nom = "+QuotedStr(asRef);
                    break;
                case CONTIENT:
                    asFiltre += asWhere+" ref_nom like "+QuotedStr("%"+asRef+"%");
                    break;
                case COMMENCE_PAR:
                    asFiltre += asWhere+" ref_nom like "+QuotedStr(asRef+"%");
                    break;
            }
        }
        //Designation
        if(!asDes.IsEmpty()) {
            switch(nDesChoix) {
                case EGALE_A:
                    if(asFiltre.IsEmpty())
                        asFiltre += asWhere+" ref_sous_nom = "+QuotedStr(asDes);
                    else
                        asFiltre += " and ref_sous_nom = "+QuotedStr(asDes);
                    break;
                case CONTIENT:
                    if(asFiltre.IsEmpty())
                        asFiltre += asWhere+" ref_sous_nom like "+QuotedStr("%"+asDes+"%");
                    else
                        asFiltre += " and ref_sous_nom like "+QuotedStr("%"+asDes+"%");
                    break;
                case COMMENCE_PAR:
                    if(asFiltre.IsEmpty())
                        asFiltre += asWhere+" ref_sous_nom like "+QuotedStr(asDes+"%");
                    else
                        asFiltre += " and ref_sous_nom like "+QuotedStr(asDes+"%");
                    break;
            }
        }
        //Page Web
        if(!asWeb.IsEmpty() && asWeb != SAISIR_PAGEWEB ) {
            switch(nWebChoix) {
                case EGALE_A:
                    if(nWebAttente != Attente_MAJ && nWebAttente != Attente_ND ) {
                        if(asFiltre.IsEmpty())
                            asFiltre += asWhere+" ref_adresse = "+QuotedStr(asWeb);
                        else
                            asFiltre += " and ref_adresse = "+QuotedStr(asWeb);
                    }else {
                        if(asFiltre.IsEmpty())
                            asFiltre += asWhere+" ref_attente = "+nWebAttente;
                        else
                            asFiltre += (AnsiString)" and ref_attente = "+nWebAttente;
                    }
                    break;
                case CONTIENT:
                    if(nWebAttente != Attente_MAJ && nWebAttente != Attente_ND ) {
                        if(asFiltre.IsEmpty())
                            asFiltre += asWhere+" ref_adresse like "+QuotedStr("%"+asWeb+"%");
                        else
                            asFiltre += " and ref_adresse like "+QuotedStr("%"+asWeb+"%");
                    }else {
                        if(asFiltre.IsEmpty())
                            asFiltre += asWhere+" ref_attente = "+nWebAttente;
                        else
                            asFiltre += (AnsiString)" and ref_attente = "+nWebAttente;
                    }
                    break;
                case COMMENCE_PAR:
                    if(nWebAttente != Attente_MAJ && nWebAttente != Attente_ND) {
                        if(asFiltre.IsEmpty())
                            asFiltre += asWhere+" ref_adresse like "+QuotedStr(asWeb+"%");
                        else
                            asFiltre += " and ref_adresse like "+QuotedStr(asWeb+"%");
                    }else {
                        if(asFiltre.IsEmpty())
                            asFiltre += asWhere+" ref_attente = "+nWebAttente;
                        else
                            asFiltre += (AnsiString)" and ref_attente = "+nWebAttente;
                    }
                    break;
            }
        }

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        asFiltre = "";
    }
    return asFiltre;
}
//---------------------------------------------------------------------------
//                  REMPLIR LA COMBO BOX PAGE WEB
//---------------------------------------------------------------------------
void __fastcall CReference::RemplirListeWeb(TStrings * cbPage)
{
    TdmWebHelp * dmWebHelp = NULL;
    CComboAttente * Attente = NULL;
    try {
        cbPage->Clear();
        cbPage->Add(SAISIR_PAGEWEB);
        //Remplissage de la combo attente
        //-------------------------------
        Attente = new CComboAttente();
        Attente->Remplir(cbPage);
        dmWebHelp = new TdmWebHelp((TComponent*)0);
        dmWebHelp->tRefer->Active = true;
        dmWebHelp->tRefer->First();

        while(!dmWebHelp->tRefer->Eof) {
            if( m_nLogiciel != 0 ) {
                if( m_nVersion != 0 ) {
                    if(     (ntRefer(LOG_CODE) == m_nLogiciel)
                        &&  (ntRefer(VER_CODE) == m_nVersion)
                        &&  (!Trim(astRefer(REF_ADRESSE)).IsEmpty()) ) {
                        cbPage->AddObject(astRefer(REF_ADRESSE), (TObject*)ADRESSE_WEB);
                    }
                }else {
                    if(     (ntRefer(LOG_CODE) == m_nLogiciel)
                        &&  (!Trim(astRefer(REF_ADRESSE)).IsEmpty()) ) {
                        cbPage->AddObject(astRefer(REF_ADRESSE), (TObject*)ADRESSE_WEB);
                    }
                }
            }else {
                if( !Trim(astRefer(REF_ADRESSE)).IsEmpty() ) {
                    cbPage->AddObject(astRefer(REF_ADRESSE), (TObject*)ADRESSE_WEB);
                }
            }
            dmWebHelp->tRefer->Next();
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
    if(Attente) {delete Attente; Attente = NULL;}
}
//---------------------------------------------------------------------------
//                      IMPORTER DES REFERENCES
//---------------------------------------------------------------------------
void __fastcall CReference::Importer()
{
    TfImport * fImport = NULL;
    try {

        fImport = new TfImport(m_nLogiciel, m_nVersion, Application);
        fImport->ShowModal();

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(fImport) {delete fImport; fImport = NULL;}
}
//---------------------------------------------------------------------------
//  Vérifier si toutes les désignations sont bien remplies
//      Valeur de retour true si il manque une designation
//                       false si toutes les designations sont remplies
//---------------------------------------------------------------------------
bool __fastcall CReference::VerifierDesignation(const AnsiString &asRef)
{
    bool bRetour = false;
    TdmWebHelp * dmWebHelp = NULL;
    TZZMySqlQuery * MaRequete = NULL;
    try {
        dmWebHelp = new TdmWebHelp((TComponent*)0);
        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text =  (AnsiString)"select ref_sous_nom,"+
                                                    "ref_attente "
                                            "from refer "+
                                            "where ref_nom = "+QuotedStr(asRef);
        MaRequete->Open();
        MaRequete->First();

        while(!MaRequete->Eof) {
            if( (asRequete(0) == "") && (nRequete(1) == AUCUNE_SELECTION) ) {
                bRetour = true;
                break;
            }
            MaRequete->Next();
        }

        MaRequete->Sql->Clear();
        MaRequete->Close();

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        bRetour = false;}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
    return bRetour;
}
//---------------------------------------------------------------------------
//       Retourne l'état des adresses Web et l'insere dans la table refer
//---------------------------------------------------------------------------
void __fastcall CReference::VerifierEtat(const AnsiString &asSite, const int &nRefCode)
{
    TdmWebHelp * dmWebHelp = NULL;
    TZZMySqlQuery * MaRequete = NULL;
    CVerifInternet * Internet = NULL;
    try {
        Internet = new CVerifInternet(true);
        Internet->ConnexionAuSite(asSite);
        dmWebHelp = new TdmWebHelp((TComponent*)0);
        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text =  (AnsiString)"update refer set ref_etat ="+Internet->GetEtat()+" where ref_code ="+nRefCode;
        MaRequete->ExecSql();
        MaRequete->Sql->Clear();

        MaRequete->Sql->Text = "Commit";
        MaRequete->ExecSql();
        MaRequete->Sql->Clear();


    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    if(Internet)  {delete Internet; Internet = NULL;}
}
//---------------------------------------------------------------------------


